package tienmanh.gs.goldminer.scene;

import java.util.ArrayList;
import java.util.HashMap;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.PinchZoomDetector;
import org.andengine.input.touch.detector.PinchZoomDetector.IPinchZoomDetectorListener;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.util.GLState;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;

import tienmanh.gs.goldminer.base.BaseScene;
import tienmanh.gs.goldminer.manager.SceneManager;
import tienmanh.gs.goldminer.manager.SceneManager.SceneType;
import tienmanh.gs.goldminer.sprite.MapAnimation;
import tienmanh.gs.goldminer.util.Gold;
import tienmanh.gs.goldminer.util.Level;
import tienmanh.gs.goldminer.util.Map;
import tienmanh.gs.goldminer.util.Point;
import tienmanh.gs.goldminer.util.Star;

public class MapScene extends BaseScene implements IOnSceneTouchListener, PinchZoomDetector.IPinchZoomDetectorListener, ScrollDetector.IScrollDetectorListener
{
	
	public static MapScene INSTACE;
	public static MapScene getInstace()
	  {
	    return INSTACE;
	  }
	  
	
	private HashMap<Integer, Point> map; //= new HashMap<Integer, Point>();
	
	Gold gold = new Gold();
	Star star = new Star();
  public static	Level level = new Level();
	ArrayList<Sprite> gates = new ArrayList<Sprite>();
  private static final float MAX_ZOOM_FACTOR = 1.5F;
  private static final float MIN_ZOOM_FACTOR = 0.5F;
  private static final int RECTANGLE_DIMENSIONS = 200;
  public static int WIDTH = 800;
  public static int HEIGHT = 480;
  private ZoomCamera mCamera;
  private float mInitialTouchZoomFactor;
  private PinchZoomDetector mPinchZoomDetector;
  private ScrollDetector mScroll;
  private Sprite mapBackground;
  private Sprite userRun;
  private HUD cameraHUD;
  private Text txt_Gold,txt_Star;
  MapAnimation mapS[];
  int khobau[] ;
  public static int mapLevel;
  //Kiểm tra cho nhân vật chạy
  public static boolean isRun;
  
  
  public void initGates(){
	  khobau  = new int[]{13 , 24 , 33 , 45 , 55 , 63 , 71 , 85 , 94 , 105};
     int count = 0;
	 for (int i = 0; i < map.size(); i++) {
		 if(i+1 == khobau[count]){
			 count++;
			 mapS[i] = new MapAnimation(map.get(i+1).getPosX(), map.get(i+1).getPosY(), resourcesManager.khobauregion, vbom , i+1); 
		 }
		 else
		 mapS[i] = new MapAnimation(map.get(i+1).getPosX(), map.get(i+1).getPosY(), resourcesManager.icon_region, vbom,i+1);
		 attachChild(mapS[i]);
		 
		 this.registerTouchArea(mapS[i]);
	}
	 for (int i = 0; i < level.readLevel(); i++) {
		mapS[i].setCurrentTileIndex(1);
	}
  }
  
  public void attackEntity()
  {
    attachChild(this.mapBackground);
    initGates();
    this.userRun = new Sprite(0,0, this.resourcesManager.mapicon_region, this.vbom);
    attachChild(this.userRun);
    mapS[0].setStar(0);
    if(level.readStateRun())
    {
    	userRun.setPosition(map.get(mapLevel-1).getPosX()+20, map.get(mapLevel-1).getPosY());
    	runAnimation(level.readEGoldSP());
    }
    else
    	userRun.setPosition(map.get(mapLevel).getPosX()+20, map.get(mapLevel).getPosY());
    	level.writeStateRun(false);
  }

  public void createScene()
  {
	  map = new HashMap<Integer, Point>();
	  INSTACE = this;
	  gates = new ArrayList<Sprite>();
	  gold.init();
	  star.init();
	  level.init();
	  isRun = false;
	  mapLevel = level.readEGoldSP();
	  
		try {
			map = new Map().getDatalevel();
			 mapS = new MapAnimation[map.size()];
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    initValue();
    initSprite();
    attackEntity();
    creatHUD();
  }
  public void creatHUD(){
	  cameraHUD = new HUD();
	  AnimatedSprite nextLv = new AnimatedSprite(0, 0,resourcesManager.valueregion.getWidth(),resourcesManager.valueregion.getHeight(), resourcesManager.valueregion, vbom)
	  {

		@Override
		protected void draw(GLState pGLState, Camera pCamera) {
			// TODO Auto-generated method stub
			super.draw(pGLState, pCamera);
		}
		  
	  };
	  AnimatedSprite shop = new AnimatedSprite(720, 350,resourcesManager.shopregion.getWidth(),resourcesManager.shopregion.getHeight(), resourcesManager.shopregion, vbom){
		  @Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				float pTouchAreaLocalX, float pTouchAreaLocalY) {
			  
			// TODO Auto-generated method stub
			  if(!isRun)
			  SceneManager.getInstance().loadShopScene(engine);
			return super
					.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
		}
		  @Override
			protected void draw(GLState pGLState, Camera pCamera) {
				// TODO Auto-generated method stub
				super.draw(pGLState, pCamera);
			}
	  };
	  cameraHUD.attachChild(nextLv);
	  cameraHUD.attachChild(shop);
	  cameraHUD.registerTouchArea(shop);
	  cameraHUD.attachChild(txt_Gold);
	  cameraHUD.attachChild(txt_Star);
	  mCamera.setHUD(cameraHUD);
  }

  public void disposeScene()
  {
	  camera.setHUD(null);
  }

  public SceneType getSceneType()
  {
    return SceneType.SCENE_MAP;
  }

  public void initSprite()
  {
    this.mapBackground = new Sprite(0.0F, 0.0F, this.resourcesManager.map_region, this.vbom);
    txt_Gold = new Text(135, 20, resourcesManager.font1, "",15, vbom);
    txt_Gold.setColor(Color.YELLOW);
    txt_Gold.setText(""+gold.readEGoldSP());
    
    txt_Star = new Text(135, 45, resourcesManager.font1, "",15, vbom);
    txt_Star.setColor(Color.YELLOW);
    txt_Star.setText(""+star.numberStar());
  }

  public void initValue()
  {
    WIDTH = 800;
    HEIGHT = 480;
    this.mCamera = this.resourcesManager.camera;
    this.mCamera.setCenter(5 + WIDTH / 2, this.resourcesManager.map_region.getHeight() -200);
    setOnSceneTouchListener(this);
    this.mPinchZoomDetector = new PinchZoomDetector(this);
    this.mScroll = new ScrollDetector(this);
    this.mPinchZoomDetector.setEnabled(true);
    this.mScroll.setEnabled(true);
  }

  public void onBackKeyPressed()
  {
	  SceneManager.getInstance().loadMenuScene(engine);
  }

  public void onPauseGame()
  {
  }

  public void onPinchZoom(PinchZoomDetector paramPinchZoomDetector, TouchEvent paramTouchEvent, float paramFloat)
  {
    float f = paramFloat * this.mInitialTouchZoomFactor;
    if ((f < 1.2F) && (f > 0.6F))
      this.mCamera.setZoomFactor(f);
  }

  public void onPinchZoomFinished(PinchZoomDetector paramPinchZoomDetector, TouchEvent paramTouchEvent, float paramFloat)
  {
    float f = paramFloat * this.mInitialTouchZoomFactor;
    if ((f < 1.2F) && (f > 0.6F))
      this.mCamera.setZoomFactor(f);
  }

  public void onPinchZoomStarted(PinchZoomDetector paramPinchZoomDetector, TouchEvent paramTouchEvent)
  {
    this.mInitialTouchZoomFactor = this.mCamera.getZoomFactor();
  }

  public void onResumeGame()
  {
  }

  public boolean onSceneTouchEvent(Scene paramScene, TouchEvent paramTouchEvent)
  {
    this.mPinchZoomDetector.onTouchEvent(paramTouchEvent);
    this.mScroll.onTouchEvent(paramTouchEvent);
    return true;
  }

  public void onScroll(ScrollDetector paramScrollDetector, int paramInt, float paramFloat1, float paramFloat2)
  {
	  int i = 0;
	    if (this.mCamera.getCenterX() - WIDTH / 2 <= 0.0F)
	      i = 5;
	    if (this.mCamera.getCenterX() + WIDTH / 2 >= this.resourcesManager.map_region.getWidth()+20)
	      i = -5;
	    int j = 0;
	    if (this.mCamera.getCenterY() - HEIGHT / 2 <= 0.0F)
	      j = 5;
	    if (this.mCamera.getCenterY() + HEIGHT / 2 >= this.resourcesManager.map_region.getHeight()+20)
	      j = -5;
	    if ((this.mCamera.getCenterX() - WIDTH / 2 >= -30.0F) && (this.mCamera.getCenterX() + WIDTH / 2 <= this.resourcesManager.map_region.getWidth()+30) && (this.mCamera.getCenterY() - HEIGHT / 2 >= -30.0F) && (this.mCamera.getCenterY() + HEIGHT / 2 <= this.resourcesManager.map_region.getHeight() + 30))
	    {
	      float f1 = this.mCamera.getZoomFactor();
	      float f2 = 0.4F * -paramFloat1 / f1;
	      float f3 = 0.4F * -paramFloat2 / f1;
	      this.mCamera.offsetCenter(f2, f3);
	    }else{
	    	this.mCamera.setCenter(this.mCamera.getCenterX() + i, this.mCamera.getCenterY() + j);
	    }
  }

  public void onScrollFinished(ScrollDetector paramScrollDetector, int paramInt, float paramFloat1, float paramFloat2)
  {
	  int i = 0;
	    if (this.mCamera.getCenterX() - WIDTH / 2 <= 0.0F)
	      i = 5;
	    if (this.mCamera.getCenterX() + WIDTH / 2 >= this.resourcesManager.map_region.getWidth()+20)
	      i = -5;
	    int j = 0;
	    if (this.mCamera.getCenterY() - HEIGHT / 2 <= 0.0F)
	      j = 5;
	    if (this.mCamera.getCenterY() + HEIGHT / 2 >= this.resourcesManager.map_region.getHeight()+20)
	      j = -5;
	    if ((this.mCamera.getCenterX() - WIDTH / 2 >= -30.0F) && (this.mCamera.getCenterX() + WIDTH / 2 <= this.resourcesManager.map_region.getWidth()+30) && (this.mCamera.getCenterY() - HEIGHT / 2 >= -30.0F) && (this.mCamera.getCenterY() + HEIGHT / 2 <= this.resourcesManager.map_region.getHeight() + 30))
	    {
	      float f1 = this.mCamera.getZoomFactor();
	      float f2 = 0.4F * -paramFloat1 / f1;
	      float f3 = 0.4F * -paramFloat2 / f1;
	      this.mCamera.offsetCenter(f2, f3);
	    }else{
	    	this.mCamera.setCenter(this.mCamera.getCenterX() + i, this.mCamera.getCenterY() + j);
	    }
  }

  public void onScrollStarted(ScrollDetector paramScrollDetector, int paramInt, float paramFloat1, float paramFloat2)
  {
	      float f1 = this.mCamera.getZoomFactor();
	      float f2 = 0.4F * -paramFloat1 / f1;
	      float f3 = 0.4F * -paramFloat2 / f1;
	      this.mCamera.offsetCenter(f2, f3);
  }

  public void runAnimation(int toPo)
  {
	  level.writeGoldSP(toPo);
	   SequenceEntityModifier entityModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {
					@Override
					public void onModifierStarted(
							final IModifier<IEntity> pModifier,
							final IEntity pItem) {
						isRun  = true;
					}

					@Override
					public void onModifierFinished(
							final IModifier<IEntity> pEntityModifier,
							final IEntity pEntity) {
						isRun = false;
					}
				},
				new LoopEntityModifier(
						new PathModifier(2.0F, new PathModifier.Path(2)
						      .to(userRun.getX(), userRun.getY()).
						              to(map.get(toPo).getPosX()+20, map.get(toPo).getPosY())), 1));
    userRun.registerEntityModifier(entityModifier);
  }


}

